		
	
	#ifdef OPTIMIZE_PAINT
	public static boolean need_repaint = true;
	#endif
		
	public Menu mainMenu;
	public boolean canContinue = false;
		
	public void UpdateMainMenu()
	{
		#ifdef OPTIMIZE_PAINT
		if(need_repaint)
		#endif
		if(splashImg == null)
		{
			try
			{
				splashImg = Image.createImage(SPRITES.SPLASH_IMAGE);
			}
			catch(Exception e)
			{
			}
		}
		
		cGame_g.drawImage(splashImg, LOGO.SPLASH_X, LOGO.SPLASH_Y, 0);
		
		drawSoftKeys(SOFTKEYS.STR_SELECT, SOFTKEYS.STR_NONE);
		
		if(mainMenu==null)
		{
			#ifdef USE_RMS
			try
			{
				rs = RecordStore.openRecordStore(SAVE.RMS_NAME, false);
				if(rs.getNextRecordID() > 1)
				{
					canContinue = true;
				}
				rs.closeRecordStore();
			}
			catch(Exception e)
			{
				DEBUG("ERROR: " + e.toString());
				canContinue = false;
			}
			#endif
			mainMenu = new Menu(SCREEN_WIDTH, SCREEN_HEIGHT, MENUS.MAIN_MENU_BACKGROUND_COLOR, MENUS.MAIN_MENU_FOREGROUND_COLOR, MENUS.MAIN_MENU_HIGHLIGHT_COLOR, 0, TEXT.MENU_ENTRIES[((canContinue)? 1 : 0)], cGame_g);
			mainMenu.setAlpha(MENUS.MAIN_MENU_ALPHA_LEVEL);
		}
		
		mainMenu.handleInput(keyflags);
		#ifdef USE_DRAWRGB
		mainMenu.draw(Menu.RECT_TYPE_ALPHA);
		#else
		mainMenu.draw(Menu.RECT_TYPE_PLAIN);
		#endif
		
		if(isKeyPressed(KEYS.KEY_FIRE) || isKeyPressed(KEYS.KEY_5) || isKeyPressed(KEYS.KEY_LSK))
		{
			int index = 0;
			
			if(canContinue)
			{
				index = mainMenu.selIndex;
			}
			else
			{
				index = mainMenu.selIndex + 1;
			}
			
			switch(index)
			{
				case MENUS.MAIN_MENU_CONTINUE:
					SetState(STATES.INGAME);
					SetIngameState(STATES.INGAME_LOAD_MATCH);
					clearKeys();
				break;
				case MENUS.MAIN_MENU_PLAY:
					mainMenu = null;
					playerName = ""; // TODO: confirmation screen
					selRulesIndex = 0;
					selPlayerIndex = 0;
					selectedPlayer = 0;
					selectedColor = 0;
					clearKeys();
					SetState(STATES.INPUT_NAME);
				break;
				case MENUS.MAIN_MENU_OPTIONS:
					SetState(STATES.OPTIONS);
					clearKeys();
				break;
				case MENUS.MAIN_MENU_HELP:
					clearKeys();
				break;
				case MENUS.MAIN_MENU_ABOUT:
					SetState(STATES.ABOUT);
					clearKeys();
				break;
				case MENUS.MAIN_MENU_EXIT:
					clearKeys();
					exit();
				break;
			}
		}
	}
	
	public String playerName = "";
	
	public void UpdateInputName()
	{
		#ifdef OPTIMIZE_PAINT
		if(need_repaint)
		#endif
		cGame_g.drawImage(splashImg, LOGO.SPLASH_X, LOGO.SPLASH_Y, 0);
		
		playerName = input(playerName, MENUS.INPUT_NAME_MAX_CHARS);
		
		#ifdef USE_DRAWRGB
		cGame_g.setColor(MENUS.MAIN_MENU_BACKGROUND_COLOR);
		cGame_g.drawAlphaRect(MENUS.INPUT_BOX_X, MENUS.INPUT_BOX_Y, MENUS.INPUT_BOX_W, MENUS.INPUT_BOX_H, MENUS.MAIN_MENU_ALPHA_LEVEL);
		#else
		cGame_g.setColor(MENUS.MAIN_MENU_BACKGROUND_COLOR);
		cGame_g.fillRect(MENUS.INPUT_BOX_X, MENUS.INPUT_BOX_Y, MENUS.INPUT_BOX_W, MENUS.INPUT_BOX_H);
		cGame_g.setColor(MENUS.MAIN_MENU_FOREGROUND_COLOR);
		cGame_g.drawRect(MENUS.INPUT_BOX_X - 1, MENUS.INPUT_BOX_Y - 1, MENUS.INPUT_BOX_W + 1, MENUS.INPUT_BOX_H + 1);
		#endif
		
		cGame_g.setFontColor(MENUS.MAIN_MENU_FOREGROUND_COLOR);
		#if SCREEN_WIDTH==128
		cGame_g.setFont(DGraph.FONT_SMALL);
		#endif
		cGame_g.drawString(TEXT.ENTER_YOUR_NAME, MENUS.INPUT_BOX_STRING_X, MENUS.INPUT_BOX_STRING_Y - cGame_g.getFontHeight() / 2, DSmallFont.HCENTER | DSmallFont.VCENTER);
		#if SCREEN_WIDTH==128
		cGame_g.drawString(playerName + ((playerName.length() < MENUS.INPUT_NAME_MAX_CHARS && ((System.currentTimeMillis() / 500) % 2 == 0))? "_" : ""), MENUS.INPUT_BOX_STRING_X + ((playerName.length() < MENUS.INPUT_NAME_MAX_CHARS && ((System.currentTimeMillis() / 500) % 2 == 0))? (/*font.getFontWidth() +*/ cGame_g.getFontPixelSize() * 2) : 0), MENUS.INPUT_BOX_STRING_Y + cGame_g.getFontHeight() / 2 + cGame_g.getFontPixelSize() * 2, DSmallFont.HCENTER | DSmallFont.VCENTER);
		#else
		cGame_g.drawString(playerName + ((playerName.length() < MENUS.INPUT_NAME_MAX_CHARS && ((System.currentTimeMillis() / 500) % 2 == 0))? "_" : ""), MENUS.INPUT_BOX_STRING_X + ((playerName.length() < MENUS.INPUT_NAME_MAX_CHARS && ((System.currentTimeMillis() / 500) % 2 == 0))? (/*font.getFontWidth() +*/ cGame_g.getFontPixelSize() * 2) : 0), MENUS.INPUT_BOX_STRING_Y + cGame_g.getFontHeight() / 2 + cGame_g.getFontPixelSize(), DSmallFont.HCENTER | DSmallFont.VCENTER);
		#endif
		#if SCREEN_WIDTH==128
		cGame_g.setFont(DGraph.FONT_BIG);
		#endif
		
		drawSoftKeys(SOFTKEYS.STR_NEXT, SOFTKEYS.STR_BACK);
		
		if(isKeyPressed(KEYS.KEY_LSK))
		{
			if(prev_game_state == STATES.CUSTOMIZE_PLAYERS)
			{
				tegPlayers[selectedPlayer].name = playerName;
				SetState(STATES.CUSTOMIZE_PLAYERS);
			}
			else
				SetState(STATES.SELECT_RULES);
			clearKeys();
		}
		else if(isKeyPressed(KEYS.KEY_RSK))
		{
			SetState(STATES.MAIN_MENU);
			clearKeys();
		}
	}
	
	public static int[] rules;
	public static int selRulesIndex = 0;
	
	public void UpdateSelectRules()
	{	
		#ifdef OPTIMIZE_PAINT
		if(need_repaint)
		#endif
		cGame_g.drawImage(splashImg, LOGO.SPLASH_X, LOGO.SPLASH_Y, 0);
		
		#ifdef USE_DRAWRGB
		cGame_g.setColor(MENUS.MAIN_MENU_BACKGROUND_COLOR);
		cGame_g.drawAlphaRect(MENUS.RULES_BOX_X, MENUS.RULES_BOX_Y, MENUS.RULES_BOX_W, MENUS.RULES_BOX_H, MENUS.MAIN_MENU_ALPHA_LEVEL);
		#else
		cGame_g.setColor(MENUS.MAIN_MENU_BACKGROUND_COLOR);
		cGame_g.fillRect(MENUS.RULES_BOX_X, MENUS.RULES_BOX_Y, MENUS.RULES_BOX_W, MENUS.RULES_BOX_H);
		cGame_g.setColor(MENUS.MAIN_MENU_FOREGROUND_COLOR);
		cGame_g.drawRect(MENUS.RULES_BOX_X - 1, MENUS.RULES_BOX_Y - 1, MENUS.RULES_BOX_W + 1, MENUS.RULES_BOX_H + 1);
		#endif
		
		cGame_g.setFont(DGraph.FONT_BIG);
		drawSoftKeys(SOFTKEYS.STR_NEXT, SOFTKEYS.STR_BACK);
		
		cGame_g.setFont(DGraph.FONT_SMALL);
		for(int i = 0; i < TEXT.SELECT_RULES.length; i++)
		{
			if(selRulesIndex != i)
			{
				cGame_g.setFontColor(MENUS.MAIN_MENU_FOREGROUND_COLOR);
				cGame_g.drawString(TEXT.SELECT_RULES[i] + rules[i], SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 - (TEXT.SELECT_RULES.length * cGame_g.getFontHeight() + TEXT.SELECT_RULES.length * cGame_g.getFontPixelSize()) / 2 +  i * (cGame_g.getFontHeight() + cGame_g.getFontPixelSize()), DSmallFont.HCENTER | DSmallFont.TOP);
			}
			else
			{
				cGame_g.setFontColor(MENUS.MAIN_MENU_HIGHLIGHT_COLOR);
				cGame_g.drawString("< " + TEXT.SELECT_RULES[i] + rules[i] + " >", SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 - (TEXT.SELECT_RULES.length * cGame_g.getFontHeight() + TEXT.SELECT_RULES.length * cGame_g.getFontPixelSize()) / 2 +  i * (cGame_g.getFontHeight() + cGame_g.getFontPixelSize()), DSmallFont.HCENTER | DSmallFont.TOP);
			}
		}
		
		if(isKeyPressed(KEYS.KEY_UP) && selRulesIndex > 0)
		{
			selRulesIndex--;
			clearKeys();
		}
		else if(isKeyPressed(KEYS.KEY_DOWN) && selRulesIndex < rules.length - 1)
		{
			selRulesIndex++;
			clearKeys();
		}
		else if(isKeyPressed(KEYS.KEY_LEFT) && rules[selRulesIndex] > RULES.MIN_VALUES[selRulesIndex])
		{
			rules[selRulesIndex]--;
			clearKeys();
		}
		else if(isKeyPressed(KEYS.KEY_RIGHT) && rules[selRulesIndex] < RULES.MAX_VALUES[selRulesIndex])
		{
			rules[selRulesIndex]++;
			clearKeys();
		}
		else if(isKeyPressed(KEYS.KEY_LSK))
		{
			cPlayer.totalPlayers = 0;
			tegPlayers = new cPlayer[rules[RULES.MATCH_PLAYERS]];
			boolean objAssigned = false;
			int obj = -1;
			
			for(int j = 0; j < tegPlayers.length; j++)
			{
				if(j != 0)
					tegPlayers[j] = new cPlayer();
				else
				{
					tegPlayers[j] = new cPlayer(playerName);
					tegPlayers[j].NPC = false;
				}
				tegPlayers[j].armies = 5;
				
				do
				{
					objAssigned = false;
					do
					{
						obj = random.nextInt() % ((rules[RULES.DESTROY_OBJECTIVES] == 0)? (RULES.OBJECTIVES.length - 6) : RULES.OBJECTIVES.length);
					}
					while(obj < 0);
					
					for(int k = j; k >= 0; k--)
					{
						if(obj == tegPlayers[k].objectiveId)
						{
							objAssigned = true;
							break;
						}
					}
				}
				while(objAssigned);
				
				tegPlayers[j].objectiveId = obj;
			}			
			SetState(STATES.CUSTOMIZE_PLAYERS);
			clearKeys();
		}
		else if(isKeyPressed(KEYS.KEY_RSK))
		{
			cGame_g.setFont(DGraph.FONT_BIG);
			SetState(STATES.INPUT_NAME);
			clearKeys();
		}
	}
	
	public int selPlayerIndex = 0;
	public int selectedPlayer = 0;
	public int selectedColor = 0;
	
	public void UpdateCustomizePlayers()
	{
		#ifdef OPTIMIZE_PAINT
		if(need_repaint)
		#endif
		cGame_g.drawImage(splashImg, LOGO.SPLASH_X, LOGO.SPLASH_Y, 0);
		
		cGame_g.setFont(DGraph.FONT_BIG);
		
		switch(selPlayerIndex)
		{
			case 2:
				drawSoftKeys(SOFTKEYS.STR_EDIT, SOFTKEYS.STR_BACK);
			break;
			default:
				drawSoftKeys(SOFTKEYS.STR_NEXT, SOFTKEYS.STR_BACK);
			break;
		}
		
		//font.setPixelSize(1);
		
		#ifdef USE_DRAWRGB
		cGame_g.setColor(MENUS.MAIN_MENU_BACKGROUND_COLOR);
		cGame_g.drawAlphaRect(MENUS.CUSTOMIZE_PLAYERS_BOX_X, MENUS.CUSTOMIZE_PLAYERS_BOX_Y, MENUS.CUSTOMIZE_PLAYERS_BOX_W, MENUS.CUSTOMIZE_PLAYERS_BOX_H, MENUS.MAIN_MENU_ALPHA_LEVEL);
		#else
		cGame_g.setColor(MENUS.MAIN_MENU_BACKGROUND_COLOR);
		cGame_g.fillRect(MENUS.CUSTOMIZE_PLAYERS_BOX_X, MENUS.CUSTOMIZE_PLAYERS_BOX_Y, MENUS.CUSTOMIZE_PLAYERS_BOX_W, MENUS.CUSTOMIZE_PLAYERS_BOX_H);
		cGame_g.setColor(MENUS.MAIN_MENU_FOREGROUND_COLOR);
		cGame_g.drawRect(MENUS.CUSTOMIZE_PLAYERS_BOX_X, MENUS.CUSTOMIZE_PLAYERS_BOX_Y, MENUS.CUSTOMIZE_PLAYERS_BOX_W, MENUS.CUSTOMIZE_PLAYERS_BOX_H);
		#endif
		
		for(int i = 0; i < TEXT.CUSTOMIZE_PLAYERS.length; i++)
		{
			switch(i)
			{
				case 0:
					if(selPlayerIndex != i)
					{
						cGame_g.setFontColor(MENUS.MAIN_MENU_FOREGROUND_COLOR);
						cGame_g.drawString(TEXT.CUSTOMIZE_PLAYERS[i] + (selectedPlayer + 1), SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 - (TEXT.CUSTOMIZE_PLAYERS.length * cGame_g.getFontHeight() + TEXT.CUSTOMIZE_PLAYERS.length * cGame_g.getFontPixelSize()) / 2 +  i * (cGame_g.getFontHeight() + cGame_g.getFontPixelSize()), DSmallFont.HCENTER | DSmallFont.TOP);
					}
					else
					{
						cGame_g.setFontColor(MENUS.MAIN_MENU_HIGHLIGHT_COLOR);
						cGame_g.drawString("< " + TEXT.CUSTOMIZE_PLAYERS[i] + (selectedPlayer + 1) + " >", SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 - (TEXT.CUSTOMIZE_PLAYERS.length * cGame_g.getFontHeight() + TEXT.CUSTOMIZE_PLAYERS.length * cGame_g.getFontPixelSize()) / 2 +  i * (cGame_g.getFontHeight() + cGame_g.getFontPixelSize()), DSmallFont.HCENTER | DSmallFont.TOP);
					}
				break;
				
				case 2:
					if(selPlayerIndex != i)
					{
						cGame_g.setFontColor(MENUS.MAIN_MENU_FOREGROUND_COLOR);
						cGame_g.drawString(TEXT.CUSTOMIZE_PLAYERS[i] + tegPlayers[selectedPlayer].getName(), SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 - (TEXT.CUSTOMIZE_PLAYERS.length * cGame_g.getFontHeight() + TEXT.CUSTOMIZE_PLAYERS.length * cGame_g.getFontPixelSize()) / 2 +  i * (cGame_g.getFontHeight() + cGame_g.getFontPixelSize()), DSmallFont.HCENTER | DSmallFont.TOP);
					}
					else
					{
						cGame_g.setFontColor(MENUS.MAIN_MENU_HIGHLIGHT_COLOR);
						cGame_g.drawString(TEXT.CUSTOMIZE_PLAYERS[i] + tegPlayers[selectedPlayer].getName(), SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 - (TEXT.CUSTOMIZE_PLAYERS.length * cGame_g.getFontHeight() + TEXT.CUSTOMIZE_PLAYERS.length * cGame_g.getFontPixelSize()) / 2 +  i * (cGame_g.getFontHeight() + cGame_g.getFontPixelSize()), DSmallFont.HCENTER | DSmallFont.TOP);
					}
				break;
				
				case 3:
					String _color = "";
					switch(tegPlayers[selectedPlayer].color)
					{
						case RULES.BLACK:
							_color = "BLACK";
						break;
						case RULES.GREEN:
							_color = "GREEN";
						break;
						case RULES.RED:
							_color = "RED";
						break;
						case RULES.MAGENTA:
							_color = "MAGENTA";
						break;
						case RULES.YELLOW:
							_color = "YELLOW";
						break;
						case RULES.BLUE:
							_color = "BLUE";
						break;
					}
					if(selPlayerIndex != i)
					{
						cGame_g.setFontColor(MENUS.MAIN_MENU_FOREGROUND_COLOR);
						cGame_g.drawString(TEXT.CUSTOMIZE_PLAYERS[i] + _color, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 - (TEXT.CUSTOMIZE_PLAYERS.length * cGame_g.getFontHeight() + TEXT.CUSTOMIZE_PLAYERS.length * cGame_g.getFontPixelSize()) / 2 +  i * (cGame_g.getFontHeight() + cGame_g.getFontPixelSize()), DSmallFont.HCENTER | DSmallFont.TOP);
					}
					else
					{
						cGame_g.setFontColor(MENUS.MAIN_MENU_HIGHLIGHT_COLOR);
						cGame_g.drawString("< " + TEXT.CUSTOMIZE_PLAYERS[i] + _color + " >", SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 - (TEXT.CUSTOMIZE_PLAYERS.length * cGame_g.getFontHeight() + TEXT.CUSTOMIZE_PLAYERS.length * cGame_g.getFontPixelSize()) / 2 +  i * (cGame_g.getFontHeight() + cGame_g.getFontPixelSize()), DSmallFont.HCENTER | DSmallFont.TOP);
					}
				break;
				
				case 4:
					if(selPlayerIndex != i)
					{
						cGame_g.setFontColor(MENUS.MAIN_MENU_FOREGROUND_COLOR);
						cGame_g.drawString(TEXT.CUSTOMIZE_PLAYERS[i] + ((tegPlayers[selectedPlayer].isNPC())? "NO" : "YES"), SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 - (TEXT.CUSTOMIZE_PLAYERS.length * cGame_g.getFontHeight() + TEXT.CUSTOMIZE_PLAYERS.length * cGame_g.getFontPixelSize()) / 2 +  i * (cGame_g.getFontHeight() + cGame_g.getFontPixelSize()), DSmallFont.HCENTER | DSmallFont.TOP);
					}
					else
					{
						cGame_g.setFontColor(MENUS.MAIN_MENU_HIGHLIGHT_COLOR);
						cGame_g.drawString("< " + TEXT.CUSTOMIZE_PLAYERS[i] + ((tegPlayers[selectedPlayer].isNPC())? "NO" : "YES") + " >", SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 - (TEXT.CUSTOMIZE_PLAYERS.length * cGame_g.getFontHeight() + TEXT.CUSTOMIZE_PLAYERS.length * cGame_g.getFontPixelSize()) / 2 +  i * (cGame_g.getFontHeight() + cGame_g.getFontPixelSize()), DSmallFont.HCENTER | DSmallFont.TOP);
					}
				break;
			}
		}
		
		if(isKeyPressed(KEYS.KEY_UP) && selPlayerIndex > 0)
		{
			selPlayerIndex--;
			if(selPlayerIndex==1)
				selPlayerIndex--;
			clearKeys();
		}
		else if(isKeyPressed(KEYS.KEY_DOWN) && selPlayerIndex < TEXT.CUSTOMIZE_PLAYERS.length - 1)
		{
			selPlayerIndex++;
			if(selPlayerIndex==1)
				selPlayerIndex++;
			clearKeys();
		}
		else if(isKeyPressed(KEYS.KEY_LEFT))
		{
			switch(selPlayerIndex)
			{
				case 0:
					if(selectedPlayer > 0)
						selectedPlayer--;
				break;
				
				case 3:
					if(selectedColor > 0)
						selectedColor--;
						
					for(int j = 0; j < tegPlayers.length; j++)
					{
						if(selectedColor < 0)
							selectedColor += RULES.PLAYER_COLORS.length;

						if(RULES.PLAYER_COLORS[selectedColor] == tegPlayers[j].color)
							selectedColor--;
					}
					
					tegPlayers[selectedPlayer].color = RULES.PLAYER_COLORS[selectedColor];
				break;
				
				case 4:
					tegPlayers[selectedPlayer].NPC = !tegPlayers[selectedPlayer].NPC;
				break;
			}
			clearKeys();
		}
		else if(isKeyPressed(KEYS.KEY_RIGHT))
		{
			switch(selPlayerIndex)
			{
				case 0:
					if(selectedPlayer < cPlayer.totalPlayers - 1)
						selectedPlayer++;
				break;
				
				case 3:
					if(selectedColor < RULES.PLAYER_COLORS.length - 1)
						selectedColor++;
					
					for(int j = 0; j < tegPlayers.length; j++)
					{
						if(selectedColor >= RULES.PLAYER_COLORS.length - 1)
							selectedColor -= RULES.PLAYER_COLORS.length - 1;
							
						if(RULES.PLAYER_COLORS[selectedColor] == tegPlayers[j].color)
							selectedColor++;												
					}
					
					tegPlayers[selectedPlayer].color = RULES.PLAYER_COLORS[selectedColor];
				break;
				
				case 4:
					tegPlayers[selectedPlayer].NPC = !tegPlayers[selectedPlayer].NPC;
				break;
			}
			clearKeys();
		}
		else if(isKeyPressed(KEYS.KEY_LSK))
		{
			if(selPlayerIndex != 2)
				SetState(STATES.INGAME);
			else
			{
				cGame_g.setFont(DGraph.FONT_BIG);
				playerName = tegPlayers[selectedPlayer].name;
				SetState(STATES.INPUT_NAME);
			}
			clearKeys();
		}
		else if(isKeyPressed(KEYS.KEY_RSK))
		{
			cGame_g.setFont(DGraph.FONT_BIG);
			SetState(STATES.SELECT_RULES);
			clearKeys();
		}
	}